﻿unit MainClientFrm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.ExtCtrls, System.Actions,
  Vcl.ActnList, Vcl.ImgList, Vcl.ComCtrls, Vcl.StdCtrls, Vcl.Buttons,
  ContextFactory, MemBuffer, Protocol, DBClientContext, Vcl.FileCtrl,
  Vcl.Grids, Vcl.DBGrids, Data.DB, Datasnap.DBClient, TCPClient, IDBClient,
  Data.Win.ADODB, TBCommon, Datasnap.Provider;

type
  TFrmMainClient = class(TForm)
    sBarMain: TStatusBar;
    panNavigation: TPanel;
    spStart: TSpeedButton;
    spStop: TSpeedButton;
    Panel4: TPanel;
    panContext: TPanel;
    pageServer: TPageControl;
    tabSysParameters: TTabSheet;
    GroupBox1: TGroupBox;
    LabelGatePort: TLabel;
    Label3: TLabel;
    crsLocalPort: TEdit;
    tabLog: TTabSheet;
    panTitle: TPanel;
    Label32: TLabel;
    Label31: TLabel;
    LB_TotalRecv: TLabel;
    LB_TotalSend: TLabel;
    cboServerIP: TComboBox;
    TabSheet1: TTabSheet;
    DBGrid1: TDBGrid;
    Panel1: TPanel;
    SpeedButton1: TSpeedButton;
    FClient: TDBClient;
    DataSource1: TDataSource;
    Edit1: TEdit;
    SpeedButton2: TSpeedButton;
    ClientDataSet1: TClientDataSet;
    procedure spStartClick(Sender: TObject);
    procedure spStopClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    function FClientFNewClientObj: TIClientContext;
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmMainClient: TFrmMainClient;

implementation

{$R *.dfm}

uses FTPClientContext;

procedure TFrmMainClient.FormCreate(Sender: TObject);
begin
  LB_TotalRecv.Caption := '0';
  LB_TotalSend.Caption := '0';
end;

procedure TFrmMainClient.SpeedButton1Click(Sender: TObject);
begin
  Self.FClient.ReadDataset(ClientDataSet1, Edit1.Text);
  if ClientDataSet1.Active then
    sBarMain.Panels[1].Text := IntToStr(ClientDataSet1.RecordCount);
end;

procedure TFrmMainClient.SpeedButton2Click(Sender: TObject);
begin
  Self.FClient.CommitDataSet('SysList', ';Class_;Code_', ClientDataSet1);
end;

procedure TFrmMainClient.spStartClick(Sender: TObject);
//var
//  sStr: string;
begin
  FClient.Host := Trim(cboServerIP.Text);
  FClient.Port := StrToInt(crsLocalPort.Text);
  FClient.BuffCount := 100;
  FClient.ThreadCount := 1;
  spStart.Enabled := not FClient.Start;
  spStop.Enabled := not spStart.Enabled;
  if not spStart.Enabled then
  begin
    sBarMain.Panels[0].Text := '启动';
//    sStr := 'Hello, iocp server!This is client!';
//    FClient.ClientContext.SendStream(@sStr[1], Length(sStr));
  end;
end;

procedure TFrmMainClient.spStopClick(Sender: TObject);
begin
  FClient.Stop;
  spStart.Enabled:= True;
  spStop.Enabled := not spStart.Enabled;
  sBarMain.Panels[0].Text := '停止';
end;

function TFrmMainClient.FClientFNewClientObj: TIClientContext;
begin
  Result := TIDBClientContext.Create(FClient.TCPEngine);
end;

end.
